/**
 * 软件著作权：东方汇创
 *
 * 系统名称：  工信部OA
 *
 * 文件名称：  GeneralServiceController.java
 *
 * 功能描述：  通用业务表Controller
 * 
 * 版本历史：
 * 
 * 2017-02-15   1.0.0版 （龙色波）（创建文件）
 */
package com.dfhc.bus.generalservice.web;



import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.activiti.engine.RepositoryService;
import org.quickbundle.base.beans.factory.RmBeanFactory;
import org.quickbundle.base.web.page.RmPageVo;
import org.quickbundle.tools.helper.RmJspHelper;
import org.quickbundle.tools.helper.RmVoHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;
import com.dfhc.ISystemConstant;
import com.dfhc.PjException;
import com.dfhc.bus.businessdataitems.service.BusinessDataItemsService;
import com.dfhc.bus.businessdataitems.vo.BusinessDataItemsVo;
import com.dfhc.bus.generalservice.IGeneralServiceConstants;
import com.dfhc.bus.generalservice.service.GeneralServiceService;
import com.dfhc.bus.generalservice.vo.GeneralServiceVo;
import com.dfhc.bus.tablecatalog.service.TableCatalogService;
import com.dfhc.bus.tablecatalog.vo.TableCatalogVo;
import com.dfhc.pub.concurrencycontrol.RequestConcurrentRoute;
import com.dfhc.pub.service.ExcelService;
import com.dfhc.pub.service.FileLockService;
import com.dfhc.pub.service.IExtGeneralService;
import com.dfhc.util.FileOperateHelper;
import com.dfhc.util.StringHelper;

/**
 * list                  /generalservice
 * insert page      GET  /generalservice/insert
 * insert action    POST /generalservice/insert
 * update page      GET  /generalservice/update/{id}
 * update action    POST /generalservice/update
 * delete action    POST /generalservice/delete
 * detail                /generalservice/detail/{id}

 * reference             /generalservice/reference
 * import page      GET  /generalservice/import
 * import action    POST /generalservice/import
 * export custom    GET  /generalservice/export
 * export action    POST /generalservice/export
 */

/**
 * 通用业务表 Controller
 * 
 * @author 龙色波
 * @see 参见的类
 */
@Controller
@RequestMapping(value = "/generalservice")
public class GeneralServiceController implements IGeneralServiceConstants {

    @Autowired
    /**
     * 通用业务表服务
     */
    private GeneralServiceService generalServiceService;
    @Autowired
    private TableCatalogService tableCatalogService; 
    @Autowired
    RepositoryService repositoryService;
    @Autowired
    private BusinessDataItemsService businessDataItemsService;
    @Autowired
    private FileLockService fileLockService;
    /**
     * 简单查询，分页显示，支持表单回写
     * @param model 模型
     * @param request http请求对象
     * @return 跳转的列表jsp     
     */
    @RequestMapping(value = "")
    public String list(Model model, HttpServletRequest request) {
        model.addAttribute(ISystemConstant.RM_PARENT_CODE, request.getParameter(ISystemConstant.RM_PARENT_CODE));  //把模块代码放入request
        //尝试获取jspurl
        String jspurl = request.getParameter(ISystemConstant.PREDEFINED_JSP_URL);
        //为空，跳到默认页面
        if(StringHelper.isEmpty(jspurl)){
        	return "/qb5activiti/bus/generalservice/listGeneralService";
        }else{
        	return jspurl.trim();
        }
    }
   
    /**
     * ajax 请求列表数据
     * @param model
     * @param request
     * @return
     * @throws IOException 
     */
    @RequestMapping(value = "ajaxList")
    public void ajaxList(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException{
    	boolean bLock = false;
    	try{
	    	bLock = RequestConcurrentRoute.getSingleton().lock(request.getParameter(ISystemConstant.JSGUID));    

    	String serviceBeanId = request.getParameter(ISystemConstant.PREDEFINED_SERVICE_BEAN_ID);
    	//如果没传了service bean
    	if(StringHelper.isEmpty(serviceBeanId)){
	        Map<String,Object> dataMap=new HashMap<String,Object>();
	        
	        String page=request.getParameter("page")==null?"1":request.getParameter("page");
	        String rows=request.getParameter("rows")==null?ISystemConstant.DEFAULT_PAGE_SIZE:request.getParameter("rows");
	        int rowsInt=Integer.parseInt(rows);//页大小
	        int pageInt=Integer.parseInt(page);//当前页
	        Map<String, Object> searchPara = getQueryCondition(request);  //从request中获得查询条件
	        RmPageVo pageVo = RmJspHelper.transctPageVo(request, generalServiceService.getCount(searchPara));
	        pageVo.setPageSize(rowsInt);
	        pageVo.setCurrentPage(pageInt);
	        String orderStr = RmJspHelper.getOrderStr(request);  //得到排序信息
	        
	        List<GeneralServiceVo> beans = generalServiceService.list(searchPara, orderStr, pageVo.getStartIndex(), pageVo.getPageSize());  //按条件查询全部,带排序
	         
	        dataMap.put("page",  pageVo.getCurrentPage());
	        dataMap.put("total",  pageVo.getPageCount());
	        dataMap.put("records",  pageVo.getRecordCount());
	        dataMap.put("rows", beans);
	         
	        response.setCharacterEncoding("UTF-8"); 
	        response.getWriter().write(JSONObject.toJSONString(dataMap));
    	}else{
    		IExtGeneralService  generalService = (IExtGeneralService) RmBeanFactory.getBean(serviceBeanId);
    		generalService.ajaxList(model,request,response);
    	}
      }finally{
  		//最后解锁
  		RequestConcurrentRoute.getSingleton().unLock(request.getParameter(ISystemConstant.JSGUID), bLock);
  	}    	
    }
    
    /**
     * 从页面表单获取信息注入vo，并插入单条记录
     * @param request http请求对象
     * @param vo 值对象
     * @return 返回Ajax应答实体对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST, 
			consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> ajaxInsert(HttpServletRequest request, @Valid GeneralServiceVo vo) {
    	boolean bLock = false;
    	try{
	    	bLock = RequestConcurrentRoute.getSingleton().lock(request.getParameter(ISystemConstant.JSGUID));    

	    	String serviceBeanId = request.getParameter(ISystemConstant.PREDEFINED_SERVICE_BEAN_ID);
	    	Map<String, Object> result = new HashMap<String, Object>();
	    	//如果没传serviceBeanId,默认方式插入
	    	if(StringHelper.isEmpty(serviceBeanId)){
		        RmVoHelper.markCreateStamp(request,vo);  //打创建时间,IP戳
		        String businessTableName = request.getParameter(ISystemConstant.PREDEFINED_BUSINESS_TABLENAME);
		        generalServiceService.insert(businessTableName,vo);  //插入单条记录
		        result.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_SUCCESS);
		        result.put(ISystemConstant.AJAX_MESSAGE, "新增成功");
	    	}else{
	    		IExtGeneralService  generalService = (IExtGeneralService) RmBeanFactory.getBean(serviceBeanId);
	    		result = generalService.doAjaxInsert(request,vo);
	    	}
	        return result;
    	}finally{
    		//最后解锁
    		RequestConcurrentRoute.getSingleton().unLock(request.getParameter(ISystemConstant.JSGUID), bLock);
    	}
    }
    
    
    /**
     * 从页面表单获取信息注入vo，并修改单条记录
     * @param request 请求对象
     * @param vo 值对象
     * @param errors Ajax错误对象
     * @return 返回Ajax应答实体对象
     */
    @RequestMapping(value = "update", method = RequestMethod.POST, 
			consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> update(HttpServletRequest request, @Valid GeneralServiceVo vo) {
    	boolean bLock = false;
    	try{
	    	bLock = RequestConcurrentRoute.getSingleton().lock(request.getParameter(ISystemConstant.JSGUID));    
    	
    	String serviceBeanId = request.getParameter(ISystemConstant.PREDEFINED_SERVICE_BEAN_ID);
    	Map<String, Object> result = new HashMap<String, Object>();
    	//如果没传serviceBeanId ，则按默认方式更新
    	if(StringHelper.isEmpty(serviceBeanId)){
	    	RmVoHelper.markModifyStamp(request,vo);  //打修改时间,IP戳
	        String businessTableName = request.getParameter(ISystemConstant.PREDEFINED_BUSINESS_TABLENAME);
	        generalServiceService.update(businessTableName,vo);  //更新单条记录
	       
	        result.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_SUCCESS);
	        result.put(ISystemConstant.AJAX_MESSAGE, "修改成功");
    	}else{
    		IExtGeneralService  generalService = (IExtGeneralService) RmBeanFactory.getBean(serviceBeanId);
    		result = generalService.doAjaxUpdate(request,vo);
    	}
        return result;
      }finally{
  		//最后解锁
  		RequestConcurrentRoute.getSingleton().unLock(request.getParameter(ISystemConstant.JSGUID), bLock);
  	}        
    }
    /**
     * 根据id获取单条记录
     * @param request 请求对象
     * @param vo 值对象
     * @param
     * @return 返回Ajax应答实体对象 
     */
    @RequestMapping(value = "get/{id}/{businessTableName}/{dateTime}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> get(@PathVariable("id") String id,@PathVariable("businessTableName") String businessTableName, HttpServletRequest request) {
    	String serviceBeanId = request.getParameter(ISystemConstant.PREDEFINED_SERVICE_BEAN_ID);
    	Map<String, Object> result = new HashMap<String, Object>();
    	boolean bLock = false;
    	try{
	    	bLock = RequestConcurrentRoute.getSingleton().lock(request.getParameter(ISystemConstant.JSGUID));    

	    	//如果serviceBeanId为空，则默认处理
	    	if(StringHelper.isEmpty(serviceBeanId)){
		    	GeneralServiceVo bean = generalServiceService.get(businessTableName,id);
		        if(bean==null){
		            result.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_FAIL);
		            result.put(ISystemConstant.AJAX_MESSAGE, "记录没找到(id:"+id+")!");
		            return result;
		        }
		        
		        result.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_SUCCESS);
		        result.put(ISystemConstant.AJAX_BEAN, bean);
	    	}else{
	    		IExtGeneralService  generalService = (IExtGeneralService) RmBeanFactory.getBean(serviceBeanId);
	    		result = generalService.ajaxGet(id,businessTableName,request); 
	    	}
	        return result;
    	}finally{
    		//最后解锁
    		RequestConcurrentRoute.getSingleton().unLock(request.getParameter(ISystemConstant.JSGUID), bLock);
    	}
    }    
    
    /**
     * 从页面的表单获取单条记录id并删除，如request.id为空则删除多条记录（request.ids）
     * @param request http请求对象
     * @param redirectAttributes 重定向属性
     * @return 返回列表页面
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public String delete(HttpServletRequest request, RedirectAttributes redirectAttributes) {
    	boolean bLock = false;
    	try{
	    	bLock = RequestConcurrentRoute.getSingleton().lock(request.getParameter(ISystemConstant.JSGUID));    

	    	String serviceBeanId = request.getParameter(ISystemConstant.PREDEFINED_SERVICE_BEAN_ID);
	    	String tableName = request.getParameter(ISystemConstant.PREDEFINED_BUSINESS_TABLENAME);
	    	if(StringHelper.isEmpty(serviceBeanId)){
		        int deleteCount = 0;  //定义成功删除的记录数
		        String id = request.getParameter(REQUEST_ID);
		        if(id != null && id.length() > 0) {
					deleteCount = generalServiceService.deleteMultiByOne(tableName,id);
		        } else {
		            String[] ids = RmJspHelper.getArrayFromRequest(request, REQUEST_IDS); //从request获取多条记录id
		            if (ids != null && ids.length != 0) {
		                deleteCount += generalServiceService.deleteByMap(tableName,ids);  //删除多条记录
		            }
		        }
		        redirectAttributes.addFlashAttribute("message", "删除成功: " + deleteCount);
		        return "redirect:/generalservice?"+ISystemConstant.RM_PARENT_CODE+"="+request.getParameter(ISystemConstant.RM_PARENT_CODE);
	    	}else{
	    		IExtGeneralService  generalService = (IExtGeneralService) RmBeanFactory.getBean(serviceBeanId);
	    		return generalService.delete(request,redirectAttributes);
	    	}
    	}finally{
    		//最后解锁
    		RequestConcurrentRoute.getSingleton().unLock(request.getParameter(ISystemConstant.JSGUID), bLock);
    	}
    }
    /**
     * ajax删除从页面的表单获取单条记录id和表名并删除
     * @param request http请求对象
     * @param redirectAttributes 重定向属性
     * @return 返回列表页面
     */
    @RequestMapping(value = "ajaxDelete", method = RequestMethod.POST, 
	consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> ajaxDelete(HttpServletRequest request) {
    	Map<String, Object> result = new HashMap<String, Object>();
    	boolean bLock = false;
    	try{
	    	bLock = RequestConcurrentRoute.getSingleton().lock(request.getParameter(ISystemConstant.JSGUID));    

	    	String serviceBeanId = request.getParameter(ISystemConstant.PREDEFINED_SERVICE_BEAN_ID);
	    	if(StringHelper.isEmpty(serviceBeanId)){
		        String[] ids = RmJspHelper.getArrayFromRequest(request, REQUEST_IDS); //从request获取多条记录id
		        String businessTableName = request.getParameter(ISystemConstant.PREDEFINED_BUSINESS_TABLENAME);
		        if (ids != null && ids.length != 0) {
		            generalServiceService.deleteByMap(businessTableName, ids);  //删除多条记录
		            result.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_SUCCESS);
		            result.put(ISystemConstant.AJAX_MESSAGE, "删除成功!");
		        }else{
		        	result.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_FAIL);
		            result.put(ISystemConstant.AJAX_MESSAGE, "删除失败!");
		        }
	    	}else{
	    		IExtGeneralService  generalService = (IExtGeneralService) RmBeanFactory.getBean(serviceBeanId);
	    		result =  generalService.doAjaxDelete(request);
	    	}
    	}finally{
    	 //解锁
    	  RequestConcurrentRoute.getSingleton().unLock(request.getParameter(ISystemConstant.JSGUID), bLock);
    	}
        return result;
    }
    
   /**
     * 从页面的表单获取单条记录id并逻辑删除，如request.id为空则删除多条记录（request.ids）
     * @param request http请求对象
     * @param redirectAttributes 重定向属性
     * @return 返回列表页面
     */
    @RequestMapping(value = "logicDelete", method = RequestMethod.POST)
    public String deleteLogic(HttpServletRequest request, RedirectAttributes redirectAttributes) {
    	boolean bLock = false;
    	try{
	    	bLock = RequestConcurrentRoute.getSingleton().lock(request.getParameter(ISystemConstant.JSGUID));    

	        int deleteCount = 0;  //定义成功删除的记录数
	    	String serviceBeanId = request.getParameter(ISystemConstant.PREDEFINED_SERVICE_BEAN_ID);
	    	if(StringHelper.isEmpty(serviceBeanId)){
		        String id = request.getParameter(REQUEST_ID);
		        if(id != null && id.length() > 0) {
		            deleteCount = generalServiceService.deleteLogic(request, id);
		        } else {
		            String[] ids = RmJspHelper.getArrayFromRequest(request, REQUEST_IDS); //从request获取多条记录id
		            if (ids != null && ids.length != 0) {
		                deleteCount += generalServiceService.deleteLogic(request,ids);  //删除多条记录
		            }
		        }
		        redirectAttributes.addFlashAttribute("message", "删除成功: " + deleteCount);
		        String jspUrl = request.getParameter(ISystemConstant.PREDEFINED_JSP_URL);
		        if(StringHelper.isEmpty(jspUrl)){
		        	return "redirect:/generalservice?"+ISystemConstant.RM_PARENT_CODE+"="+request.getParameter(ISystemConstant.RM_PARENT_CODE);
		        }else{
		        	//判断是否包含ISystemConstant.RM_PARENT_CODE
		        	if(jspUrl.contains(ISystemConstant.RM_PARENT_CODE)){
		        		return "redirect:"+jspUrl;
		        	}else{
		        		String parentCodeParam =ISystemConstant.RM_PARENT_CODE+"="+request.getParameter(ISystemConstant.RM_PARENT_CODE); 
		        		if(jspUrl.contains("?")){
		        			jspUrl  = StringHelper.replaceAll(jspUrl, "?", "?"+parentCodeParam+"&").toString();
		        		}else{
		        			jspUrl  = jspUrl+"?"+parentCodeParam;
		        		}
		        		return "redirect:"+jspUrl;
		        	}
		        }
	    	}else{
	    		IExtGeneralService  generalService = (IExtGeneralService) RmBeanFactory.getBean(serviceBeanId);
	    		return  generalService.doLogicDelete(request,redirectAttributes);
	    	}
    	}finally{
        	//解锁
        	RequestConcurrentRoute.getSingleton().unLock(request.getParameter(ISystemConstant.JSGUID), bLock);
    	}
    }
    /**
     * 从页面的表单获取单条记录id，并察看这条记录的详细信息
     * @param id 待删除的id
     * @param model 模型
     * @request 请求对象
     * @return 返回详细信息页面
     */
    @RequestMapping(value = "detail/{id}")
    public String detail(@PathVariable("id") String id, Model model, HttpServletRequest request) {
    	String serviceBeanId = request.getParameter(ISystemConstant.PREDEFINED_SERVICE_BEAN_ID);
    	boolean bLock = false;
    	try{
	    	bLock = RequestConcurrentRoute.getSingleton().lock(request.getParameter(ISystemConstant.JSGUID));    

	    	if(StringHelper.isEmpty(serviceBeanId)){    	
		        String businessTableName;
		        businessTableName = request.getParameter(ISystemConstant.PREDEFINED_BUSINESS_TABLENAME);
				GeneralServiceVo bean = generalServiceService.get(businessTableName, id);
		        model.addAttribute(REQUEST_BEAN, bean);  //把vo放入request
		        if(RM_YES.equals(request.getParameter(REQUEST_IS_READ_ONLY))) {
		            model.addAttribute(REQUEST_IS_READ_ONLY, request.getParameter(REQUEST_IS_READ_ONLY));
		        }
		        String jspUrl = request.getParameter(ISystemConstant.PREDEFINED_JSP_URL);
		        if(StringHelper.isEmpty(jspUrl)){
		        	return "/qb5activiti/bus/generalservice/detailGeneralService";
		        }else{
		        	return jspUrl;
		        }
	    	}else{
	    		IExtGeneralService  generalService = (IExtGeneralService) RmBeanFactory.getBean(serviceBeanId);
	    		return  generalService.detail(id,model,request);
	    	}
    	}finally{
    		//最后解锁
    		RequestConcurrentRoute.getSingleton().unLock(request.getParameter(ISystemConstant.JSGUID), bLock);
    	}
    }

    /**
     * 参照信息查询，带简单查询，分页显示，支持表单回写
     * @param model 模型
     * @param request http请求对象
     * @return 返回参照列表页面
     */
    @RequestMapping(value = "reference")
    public String reference(Model model, HttpServletRequest request) {
        model.addAttribute(REQUEST_REFERENCE_INPUT_TYPE, request.getParameter(REQUEST_REFERENCE_INPUT_TYPE));  //传送输入方式,checkbox或radio
        String jspUrl = request.getParameter(ISystemConstant.PREDEFINED_JSP_URL);
        if(StringHelper.isEmpty(jspUrl)){
        	return "/qb5activiti/bus/generalservice/util/referenceGeneralService";
        }else{
        	return jspUrl;
        }
    }
    /**
     * 参照改为ajax
     * @param model
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "referenceAjax")
    public void referenceAjax(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException {
    	ajaxList(model, request, response);
    }
    /**
     * 跳转到导入页
     * @param model 模型
     * @return 返回导入页
     */
    @RequestMapping(value = "import", method = RequestMethod.GET)
    public String importDataForm(Model model) {
        return "/qb5activiti/bus/generalservice/importGeneralService";
    }
    
    /**
     * 执行导入
     * @param model 模型
     * @return 返回导入页面
     */
    @RequestMapping(value = "import", method = RequestMethod.POST)
    public String importData(Model model) {
        model.addAttribute("isSubmit", "1");
        return "/qb5activiti/bus/generalservice/importGeneralService";
    }
    
    /**
     * 定制导出
     * @param model 模型
     * @return 返回导出页面
     */
    @RequestMapping(value = "export", method = RequestMethod.GET)
    public String exportCustomForm(Model model) {
        return "/qb5activiti/bus/generalservice/exportGeneralService_custom";
    }
    
    /**
     * 执行导出
     * @param model 模型
     * @return 返回导出xls页面
     */
    @RequestMapping(value = "export", method = RequestMethod.POST)
    public String exportData(Model model) {
        return "/qb5activiti/bus/generalservice/exportGeneralService_excel";
    }

    /**
     * 从request中获得查询条件
     * @param request http请求对象
     * @return 条件Map
     */
    @SuppressWarnings("unchecked")
	public static Map<String, Object> getQueryCondition(HttpServletRequest request) {
        Map<String, Object> searchMap = null;
        if(request.getAttribute(REQUEST_QUERY_CONDITION) != null) {  //如果request.getAttribute中有，就不取request.getParameter
            searchMap = (Map<String, Object>)request.getAttribute(REQUEST_QUERY_CONDITION);
        } else {
           searchMap = new HashMap<String, Object>();
           searchMap.put("id", request.getParameter("id"));
           searchMap.put("businessGroup", request.getParameter("businessGroup"));
           searchMap.put("parentId", request.getParameter("parentId"));
           searchMap.put("businessAttribute70", request.getParameter("businessAttribute70"));
           searchMap.put("businessAttribute69", request.getParameter("businessAttribute69"));
           searchMap.put("businessAttribute68", request.getParameter("businessAttribute68"));
           searchMap.put("businessAttribute67", request.getParameter("businessAttribute67"));
           searchMap.put("businessAttribute66", request.getParameter("businessAttribute66"));
           searchMap.put("businessAttribute65", request.getParameter("businessAttribute65"));
           searchMap.put("businessAttribute64", request.getParameter("businessAttribute64"));
           searchMap.put("businessAttribute63", request.getParameter("businessAttribute63"));
           searchMap.put("businessAttribute62", request.getParameter("businessAttribute62"));
           searchMap.put("businessAttribute61", request.getParameter("businessAttribute61"));
           searchMap.put("businessAttribute60", request.getParameter("businessAttribute60"));
           searchMap.put("businessAttribute59", request.getParameter("businessAttribute59"));
           searchMap.put("businessAttribute58", request.getParameter("businessAttribute58"));
           searchMap.put("businessAttribute57", request.getParameter("businessAttribute57"));
           searchMap.put("businessAttribute56", request.getParameter("businessAttribute56"));
           searchMap.put("businessAttribute55", request.getParameter("businessAttribute55"));
           searchMap.put("businessAttribute54", request.getParameter("businessAttribute54"));
           searchMap.put("businessAttribute53", request.getParameter("businessAttribute53"));
           searchMap.put("businessAttribute52", request.getParameter("businessAttribute52"));
           searchMap.put("businessAttribute51", request.getParameter("businessAttribute51"));
           searchMap.put("businessAttribute50", request.getParameter("businessAttribute50"));
           searchMap.put("businessAttribute49", request.getParameter("businessAttribute49"));
           searchMap.put("businessAttribute48", request.getParameter("businessAttribute48"));
           searchMap.put("businessAttribute47", request.getParameter("businessAttribute47"));
           searchMap.put("businessAttribute46", request.getParameter("businessAttribute46"));
           searchMap.put("businessAttribute45", request.getParameter("businessAttribute45"));
           searchMap.put("businessAttribute44", request.getParameter("businessAttribute44"));
           searchMap.put("businessAttribute43", request.getParameter("businessAttribute43"));
           searchMap.put("businessAttribute42", request.getParameter("businessAttribute42"));
           searchMap.put("businessAttribute41", request.getParameter("businessAttribute41"));
           searchMap.put("businessAttribute40", request.getParameter("businessAttribute40"));
           searchMap.put("businessAttribute39", request.getParameter("businessAttribute39"));
           searchMap.put("businessAttribute38", request.getParameter("businessAttribute38"));
           searchMap.put("businessAttribute37", request.getParameter("businessAttribute37"));
           searchMap.put("businessAttribute36", request.getParameter("businessAttribute36"));
           searchMap.put("businessAttribute35", request.getParameter("businessAttribute35"));
           searchMap.put("businessAttribute34", request.getParameter("businessAttribute34"));
           searchMap.put("businessAttribute33", request.getParameter("businessAttribute33"));
           searchMap.put("businessAttribute32", request.getParameter("businessAttribute32"));
           searchMap.put("businessAttribute31", request.getParameter("businessAttribute31"));
           searchMap.put("businessAttribute30", request.getParameter("businessAttribute30"));
           searchMap.put("businessAttribute29", request.getParameter("businessAttribute29"));
           searchMap.put("businessAttribute28", request.getParameter("businessAttribute28"));
           searchMap.put("businessAttribute27", request.getParameter("businessAttribute27"));
           searchMap.put("businessAttribute26", request.getParameter("businessAttribute26"));
           searchMap.put("businessAttribute25", request.getParameter("businessAttribute25"));
           searchMap.put("businessAttribute24", request.getParameter("businessAttribute24"));
           searchMap.put("businessAttribute23", request.getParameter("businessAttribute23"));
           searchMap.put("businessAttribute22", request.getParameter("businessAttribute22"));
           searchMap.put("businessAttribute21", request.getParameter("businessAttribute21"));
           searchMap.put("businessAttribute20", request.getParameter("businessAttribute20"));
           searchMap.put("businessAttribute19", request.getParameter("businessAttribute19"));
           searchMap.put("businessAttribute18", request.getParameter("businessAttribute18"));
           searchMap.put("businessAttribute17", request.getParameter("businessAttribute17"));
           searchMap.put("businessAttribute16", request.getParameter("businessAttribute16"));
           searchMap.put("businessAttribute15", request.getParameter("businessAttribute15"));
           searchMap.put("businessAttribute14", request.getParameter("businessAttribute14"));
           searchMap.put("businessAttribute13", request.getParameter("businessAttribute13"));
           searchMap.put("businessAttribute12", request.getParameter("businessAttribute12"));
           searchMap.put("businessAttribute11", request.getParameter("businessAttribute11"));
           searchMap.put("businessAttribute10", request.getParameter("businessAttribute10"));
           searchMap.put("businessAttribute9", request.getParameter("businessAttribute9"));
           searchMap.put("businessAttribute8", request.getParameter("businessAttribute8"));
           searchMap.put("businessAttribute7", request.getParameter("businessAttribute7"));
           searchMap.put("businessAttribute6", request.getParameter("businessAttribute6"));
           searchMap.put("businessAttribute5", request.getParameter("businessAttribute5"));
           searchMap.put("businessAttribute4", request.getParameter("businessAttribute4"));
           searchMap.put("businessAttribute3", request.getParameter("businessAttribute3"));
           searchMap.put("businessAttribute1", request.getParameter("businessAttribute1"));
           searchMap.put("businessAttribute2", request.getParameter("businessAttribute2"));
           searchMap.put("delete_flag",ISystemConstant.DICTIONARY_RM_YES_NOT_0);
           searchMap.put(ISystemConstant.PREDEFINED_BUSINESS_TABLENAME,request.getParameter(ISystemConstant.PREDEFINED_BUSINESS_TABLENAME));
        }
        return searchMap;
    }
   /**
    * 导出通用业务表数据为xls,并下载
    * @throws Exception 
    *
    */
@RequestMapping(value="exportXls"+ISystemConstant.SUFFIX_ACTION)
public void frontExportXls(Model model,HttpServletRequest request,HttpServletResponse response) throws Exception{	
	exportXls(model, request, response);
}
/**
 * 导出通用业务表数据为xls,并下载
 * @throws Exception 
 **/
@RequestMapping(value="exportXls")
public void backExportXls(Model model,HttpServletRequest request,HttpServletResponse response) throws Exception{	
	exportXls(model, request, response);
}
/**
 * 导出xls公共方法
 * @param model
 * @param request
 * @param response
 * @throws UnsupportedEncodingException
 */
private void exportXls(Model model, HttpServletRequest request,
		HttpServletResponse response) throws UnsupportedEncodingException {
	boolean bLock = false;
	try{
    	bLock = RequestConcurrentRoute.getSingleton().lock(request.getParameter(ISystemConstant.JSGUID));    

		//获取业务表名
		String businessTableName = request.getParameter(ISystemConstant.PREDEFINED_BUSINESS_TABLENAME);
		if(StringHelper.isEmpty(businessTableName)){
			throw new PjException("缺少业务表名参数!");
		}
		//获取扩展服务bean id
		String serviceBeanId = request.getParameter(ISystemConstant.PREDEFINED_SERVICE_BEAN_ID);
		//获取排序
		String order = request.getParameter(ISystemConstant.PREDEFINED_ORDER);
		if(!StringHelper.isEmpty(order)){
	    	String order1 = new String(order.getBytes("ISO-8859-1"),"UTF-8");
	    	order = java.net.URLDecoder.decode(order1, "UTF-8");
	    }
		Map<String, Object> searchPara = new HashMap<String,Object>();
		//扩展服务bean id为空，则默认从通用业务表中取
		if(StringHelper.isEmpty(serviceBeanId)){
			searchPara.put(ISystemConstant.PREDEFINED_BUSINESS_TABLENAME, businessTableName);
	    	List<GeneralServiceVo> generalServiceVos = generalServiceService.list(searchPara, order);    		
	    	String sheetName = request.getParameter(ISystemConstant.PREDEFINED_SHELL_NAME);
	    	if(StringHelper.isEmpty(sheetName)){
	    		sheetName = ISystemConstant.DEFAULT_SHELL_NAME;
	    	}
	    	searchPara.clear();
	    	searchPara.put("englishName", businessTableName);
			List<TableCatalogVo> tableVos = tableCatalogService.list(searchPara, null);
			if(tableVos==null||tableVos.size()==0){
				throw new PjException("表英文名:"+businessTableName+"不存在!");
			}
			String tableCataLogId = tableVos.get(0).getId();
			searchPara.clear();
	    	searchPara.put("tableCatalogId", tableCataLogId);
	    	searchPara.put("delete_flag", ISystemConstant.DICTIONARY_RM_YES_NOT_0);
	    	List<BusinessDataItemsVo> dataItemsVos = businessDataItemsService.list(searchPara, "order_code asc");
	    	LinkedHashMap<String,String> fieldMap = new LinkedHashMap<String,String>();
	    	//获取删除的列名
	    	String deletes = request.getParameter(ISystemConstant.PREDEFINED_EXPORT_DELETE_COLUMNNAMES);
	    	List<String> deleteList =  new ArrayList<String>();
			if(!StringHelper.isEmpty(deletes)){
				deleteList = Arrays.asList(deletes.split(","));
			}
	    	
	    	for(BusinessDataItemsVo dataItemVo:dataItemsVos){
	    		if(deleteList.contains(StringHelper.toCamelCase(dataItemVo.getDataItemEnName()))){
	    			continue;
	    		}
	    		fieldMap.put(StringHelper.toCamelCase(dataItemVo.getDataItemEnName()), dataItemVo.getDataItemCnName());
	    	}
	    	
	    	//获取需要隐藏的列数组
	    	String hides = request.getParameter(ISystemConstant.PREDEFINED_EXPORT_HIDE_COLUMNNAMES);
	    	
			List<String> hideList =  new ArrayList<String>();
			if(!StringHelper.isEmpty(hides)){
				hideList = Arrays.asList(hides.split(","));
			}
			ExcelService.listToExcel(generalServiceVos, fieldMap, hideList,sheetName, response);
		}else{
			IExtGeneralService  generalService = (IExtGeneralService) RmBeanFactory.getBean(serviceBeanId);
			generalService.exportXls(model,request,response);
		}
	}finally{
		//最后解锁
		RequestConcurrentRoute.getSingleton().unLock(request.getParameter(ISystemConstant.JSGUID), bLock);
	}
}

/**
 * 前台导入通用业务表数据为xls到数据库
 * @throws Exception 
 **/
@RequestMapping(value="importXls"+ISystemConstant.SUFFIX_ACTION)
public void frontImportXls(HttpServletRequest request, HttpServletResponse response) throws Exception{
	importXls(request, response);
}
/**
 * 后台导入通用业务表数据为xls到数据库
 * @throws Exception 
 **/
@RequestMapping(value="importXls")
public void backImportXls(HttpServletRequest request, HttpServletResponse response) throws Exception{
	importXls(request, response);
}
/**
 * 导入xls公共方法
 * @param request
 * @param response
 */
private void importXls(HttpServletRequest request, HttpServletResponse response) {
	boolean bLock = false;
	try{
    	bLock = RequestConcurrentRoute.getSingleton().lock(request.getParameter(ISystemConstant.JSGUID));    

		try {
			//获取业务表名
			String businessTableName = request.getParameter(ISystemConstant.PREDEFINED_BUSINESS_TABLENAME);
			if(StringHelper.isEmpty(businessTableName)){
				throw new PjException("缺少业务表名参数!");
			}
			//获取扩展服务bean id
			String serviceBeanId = request.getParameter(ISystemConstant.PREDEFINED_SERVICE_BEAN_ID);
			Map<String, Object> searchPara = new HashMap<String,Object>();
			//获取上传文件xls
			List<MultipartFile> uploadFiles = FileOperateHelper.listMultipartFile(request);
			if(uploadFiles==null ||uploadFiles.isEmpty()){
				throw new PjException("缺少上传文件!");
			}	
			//扩展服务bean id为空，则默认方式导入businessTableName
			if(StringHelper.isEmpty(serviceBeanId)){
		    	String sheetName = request.getParameter(ISystemConstant.PREDEFINED_SHELL_NAME);
		    	if(StringHelper.isEmpty(sheetName)){
		    		sheetName = ISystemConstant.DEFAULT_SHELL_NAME;
		    	}
		    	searchPara.clear();
		    	searchPara.put("englishName", businessTableName);
				//根据业务表名获取表id
				List<TableCatalogVo> tableVos = tableCatalogService.list(searchPara, null);
				if(tableVos==null||tableVos.size()==0){
					throw new PjException("表英文名:"+businessTableName+"不存在!");
				}
				String tableCataLogId = tableVos.get(0).getId();
				searchPara.clear();
		    	searchPara.put("tableCatalogId", tableCataLogId);
		    	searchPara.put("delete_flag", ISystemConstant.DICTIONARY_RM_YES_NOT_0);
		    	List<BusinessDataItemsVo> dataItemsVos = businessDataItemsService.list(searchPara, "id asc");
		    	LinkedHashMap<String,String> fieldMap = new LinkedHashMap<String,String>();
		    	String idCnName=null;//id中文名
		    	for(BusinessDataItemsVo dataItemVo:dataItemsVos){
		    		//获取ID对应中文id名称
		    		if(dataItemVo.getDataItemEnName().equalsIgnoreCase(ISystemConstant.PREDEFINED_VO_ID)){
		    			idCnName = dataItemVo.getDataItemCnName();
		    		}
		    		fieldMap.put(dataItemVo.getDataItemCnName(),StringHelper.toCamelCase(dataItemVo.getDataItemEnName()));
		    	}
		    	MultipartFile mfile = uploadFiles.get(0);
		    	
				List<GeneralServiceVo> generalServiceVos =null;
				InputStream inputStream = null;
				try{
					inputStream = mfile.getInputStream();
					if(!StringHelper.isEmpty(idCnName)){
						generalServiceVos = ExcelService.excelToList(inputStream, sheetName, GeneralServiceVo.class, fieldMap, new String[]{idCnName});
					}else{
						generalServiceVos = ExcelService.excelToList(inputStream, sheetName, GeneralServiceVo.class, fieldMap, new String[]{});
					}
				}finally{
					if(inputStream!=null){
						inputStream.close();
						inputStream = null;
					}
				}
				generalServiceService.insertUpdateBatch(businessTableName, generalServiceVos); 
			}else{
				IExtGeneralService  generalService = (IExtGeneralService) RmBeanFactory.getBean(serviceBeanId);
	    		generalService.importXls(request,response);
			}	
		} 
		catch (Exception e) {
			e.printStackTrace();
			Map<String, Object> result = new HashMap<String, Object>();//
			result.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_FAIL);
			String PjException = "com.dfhc.PjException:";
			result.put(ISystemConstant.AJAX_MESSAGE, e.getMessage().substring(e.getMessage().indexOf(PjException)+PjException.length()));
			response.setContentType("text/html;charset=utf-8");
			try {
				response.getWriter().print(JSONObject.toJSONString(result));
				response.getWriter().flush();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return;
		}
		Map<String, Object> result = new HashMap<String, Object>();
		result.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_SUCCESS);
		result.put(ISystemConstant.AJAX_MESSAGE, "上传成功!");
		response.setContentType("text/html;charset=utf-8");
		try {
			response.getWriter().print(JSONObject.toJSONString(result));
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	 }finally{
		//最后解锁
		RequestConcurrentRoute.getSingleton().unLock(request.getParameter(ISystemConstant.JSGUID), bLock);
	 }
}  
/**
 * ajax逻辑删除
 * @param request http请求对象
 * @param redirectAttributes 重定向属性
 * @return 返回列表页面
 */
@RequestMapping(value = "ajaxLogicDelete", method = RequestMethod.POST, 
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map<String, Object> ajaxLogicDelete(HttpServletRequest request) {
	Map<String, Object> result = new HashMap<String, Object>();
	boolean bLock = false;
	try{
    	bLock = RequestConcurrentRoute.getSingleton().lock(request.getParameter(ISystemConstant.JSGUID));    

    	String serviceBeanId = request.getParameter(ISystemConstant.PREDEFINED_SERVICE_BEAN_ID);
    	if(StringHelper.isEmpty(serviceBeanId)){
	        String[] ids = RmJspHelper.getArrayFromRequest(request, REQUEST_IDS); //从request获取多条记录id	        
	        if (ids != null && ids.length != 0) {
	        	generalServiceService.deleteLogic(request, ids);	            
	            result.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_SUCCESS);
	            result.put(ISystemConstant.AJAX_MESSAGE, "删除成功!");
	        }else{
	        	result.put(ISystemConstant.AJAX_STATUS, ISystemConstant.AJAX_RESULT_FAIL);
	            result.put(ISystemConstant.AJAX_MESSAGE, "删除失败!");
	        }
    	}else{
    		IExtGeneralService  generalService = (IExtGeneralService) RmBeanFactory.getBean(serviceBeanId);
    		result =  generalService.doAjaxDelete(request);
    	}
	}finally{
	 //解锁
	  RequestConcurrentRoute.getSingleton().unLock(request.getParameter(ISystemConstant.JSGUID), bLock);
	}
    return result;
}  
}
